home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 February / macformat-047.iso / Shareware Plus / Developers / Flame / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-20  |  6.0 KB  |  280 lines  |  [TEXT/CWIE]

  1. #include "flame.h"
  2.  
  3. #define kDefaultRes    0x00480000
  4.  
  5.  
  6. FlamePtr FlamesDialogBox(CTabHandle fireColors, PixMapPtr thePix, 
  7.     short *odds, short *max, short *min, Boolean *fuzz, short *takeaway);
  8. void ShowNum(long theLong);
  9. long GetNum(short itemNum, DialogPtr theDialog);
  10. void SetNum(short itemNum, DialogPtr theDialog, int tempNum);
  11. void ToggleCheckMark(int itemNum, DialogPtr theDialog);
  12. Boolean GetCheckMark(int itemNum, DialogPtr theDialog);
  13. OSErr SetUpPixMap(short depth, 
  14.                 Rect *bounds, 
  15.                 CTabHandle colors, 
  16.                 PixMapHandle thePixMap);
  17.  
  18. void main(void)
  19. {
  20. WindowPtr    outWin;
  21. PixMapPtr    firePlace;
  22. OSErr        theErr;
  23. Rect        destRect;
  24. CTabHandle    fireColors,
  25.             systemColors;
  26. FlamePtr    fire;
  27. Rect        tempRect;
  28. long        start,
  29.             end, 
  30.             count = 0;
  31. short        i;
  32. short        odds, lenght, takeAway, max, min;
  33. Boolean        fuzz;
  34.  
  35. InitGraf(&qd.thePort);
  36. InitFonts();
  37. InitWindows();
  38. InitMenus();
  39. TEInit();
  40. InitDialogs(nil);
  41. InitCursor();
  42. qd.randSeed = TickCount();
  43. FlushEvents(everyEvent,0);
  44. MaxApplZone();
  45. MoreMasters();
  46. MoreMasters();
  47.  
  48. outWin = GetNewCWindow(128, nil, (WindowPtr)-1);
  49. destRect = outWin->portRect;
  50.  
  51.  
  52. fireColors = (CTabHandle)GetResource('clut', 128);
  53. HLock((Handle)fireColors);
  54.  
  55. theErr = SetUpPixMap(8, &destRect, fireColors, &firePlace);
  56.  
  57. fire = FlamesDialogBox(fireColors, firePlace, &odds, &max, &min, &fuzz, &takeAway);
  58.     
  59. if (theErr != noErr)
  60. {
  61.     ShowNum(theErr);
  62.     CloseWindow(outWin);
  63.     ExitToShell();
  64. }
  65.  
  66. ReleaseResource((Handle)fireColors);
  67.  
  68. start = TickCount();
  69. do{
  70.     count++;
  71.     SetUpBurn(odds, max, min, fire);
  72.     Burn(fire, takeAway, fuzz);
  73.     CopyFlame(0,0,fire, firePlace);
  74.     CopyBits((BitMapPtr)firePlace,
  75.             &((GrafPtr)outWin)->portBits,
  76.             &destRect, &destRect,
  77.             srcCopy, nil);
  78. }while(!Button());
  79. end = TickCount();
  80.  
  81. ShowNum(((double)count/(end - start))*60);
  82. CloseWindow(outWin);
  83. KillFlames(&fire);
  84. }
  85.  
  86. void ShowNum(long theLong)
  87. {
  88. DialogPtr    theDialog;
  89. GrafPtr        oldPort;
  90. short        item;
  91.  
  92. theDialog = GetNewDialog(128, nil, (WindowPtr)-1);
  93. GetPort(&(GrafPtr)oldPort);
  94. ShowWindow(theDialog);
  95. SelectWindow(theDialog);
  96. SetPort(theDialog);
  97. SetNum(3, theDialog, theLong);
  98. do{
  99. ModalDialog(nil, &item);
  100. }while(item != 1);
  101. CloseDialog(theDialog);
  102. SetPort(oldPort);
  103. }
  104.  
  105. FlamePtr FlamesDialogBox(CTabHandle fireColors, PixMapPtr thePix, 
  106.     short *odds, short *max, short *min, Boolean *fuzz, short *takeaway)
  107. {
  108. DialogPtr    theDialog;
  109. short        item;
  110. FlamePtr    tempFire;
  111.  
  112. theDialog = GetNewDialog(129, nil, (WindowPtr)-1);
  113. ShowWindow(theDialog);
  114. SelectWindow(theDialog);
  115. SetPort(theDialog);
  116.  
  117. do{
  118.     ModalDialog(nil, &item);
  119.     if (item == 7)
  120.         ToggleCheckMark(7, theDialog);
  121.     if (item == 6)
  122.         ToggleCheckMark(6, theDialog);
  123. }while(item != 1);
  124.  
  125. if(GetCheckMark(7, theDialog))
  126.     SetUpFlames(fireColors, thePix->pmTable, &thePix->bounds, lowRes2, &tempFire);
  127. else
  128.     SetUpFlames(fireColors, thePix->pmTable, &thePix->bounds, noSpeedUp, &tempFire);
  129.     
  130. *odds = GetNum(2, theDialog);
  131. *max = GetNum(3, theDialog);
  132. *min = GetNum(4, theDialog);
  133. *fuzz = GetCheckMark(6, theDialog);
  134. *takeaway =GetNum(5, theDialog);
  135.  
  136. CloseDialog(theDialog);
  137. return tempFire;
  138. }
  139.  
  140. void SetNum(short itemNum, DialogPtr theDialog, int tempNum)
  141. {
  142. Handle    dummyHandle;        
  143. Rect    dummyRect;
  144. short    dummyType;
  145. unsigned char    tempText[256];
  146.  
  147. NumToString(tempNum, &tempText[0]);
  148.  
  149. GetDialogItem(theDialog, itemNum, &dummyType, &dummyHandle, & dummyRect);
  150. SetDialogItemText(dummyHandle, tempText);
  151. }
  152.  
  153.  
  154. long GetNum(short itemNum, DialogPtr theDialog)
  155. {
  156. Handle    dummyHandle;        
  157. Rect    dummyRect;
  158. short    dummyType;
  159. unsigned char    tempText[256];
  160. long        tempNum;
  161.  
  162. GetDialogItem(theDialog, itemNum, &dummyType, &dummyHandle, & dummyRect);
  163. GetDialogItemText(dummyHandle, tempText);
  164.  
  165. StringToNum(tempText, &tempNum);
  166. return tempNum;
  167. }
  168.  
  169. void ToggleCheckMark(int itemNum, DialogPtr theDialog)
  170. {
  171. Handle    dummyHandle;
  172. Rect    dummyRect;
  173. short    dummyType,
  174.         theValue;
  175.         
  176. GetDialogItem(theDialog, itemNum, &dummyType, &dummyHandle, &dummyRect);
  177. theValue = GetControlValue((ControlHandle)dummyHandle);
  178. if (theValue == true)
  179. {
  180.     SetControlValue((ControlHandle)dummyHandle, false);
  181. } else {
  182.     SetControlValue((ControlHandle)dummyHandle, true);
  183. }
  184. }
  185.  
  186. Boolean GetCheckMark(int itemNum, DialogPtr theDialog)
  187. {
  188. Handle    dummyHandle;
  189. Rect    dummyRect;
  190. short    dummyType,
  191.         theValue;
  192.         
  193. GetDialogItem(theDialog, itemNum, &dummyType, &dummyHandle, & dummyRect);
  194. theValue = GetControlValue((ControlHandle)dummyHandle);
  195. if (theValue == true)
  196. {
  197.     return true;
  198. } else {
  199.     return false;
  200. }
  201. }
  202.  
  203.  
  204. OSErr SetUpPixMap(short depth, 
  205.                 Rect *bounds, 
  206.                 CTabHandle colors, 
  207.                 PixMapHandle thePixMap)
  208. {
  209. CTabHandle    newColors;
  210. Ptr            offBaseAddr;
  211. OSErr        error;
  212. short        bytesPerRow;
  213.  
  214. error = noErr;
  215. newColors = nil;
  216. offBaseAddr = nil;
  217.  
  218. *thePixMap = (PixMapPtr) NewPtr(sizeof(PixMap));
  219. if (depth <= 8)
  220. {
  221.     if (colors != nil)
  222.     {    
  223.         newColors = colors;
  224.         error = HandToHand((Handle *) &newColors);
  225.     } else {
  226.         /*asign system clut*/
  227.     }
  228. } else {
  229.     newColors = (CTabHandle)NewHandle(sizeof(ColorTable) - sizeof(CSpecArray));
  230.     error = MemError();
  231. }
  232.  
  233. if (error == noErr)
  234. {
  235.     bytesPerRow = ((((long)depth * 
  236.                 (long)(bounds->right - bounds->left)) + 15L) >> 4L) << 1L;
  237.     offBaseAddr = NewPtrClear((unsigned long)bytesPerRow * (bounds->bottom - bounds->top));
  238.     if (offBaseAddr != nil)
  239.     {
  240.         (**thePixMap).baseAddr = offBaseAddr;
  241.         (**thePixMap).rowBytes = bytesPerRow | 0x8000;
  242.         (**thePixMap).bounds = *bounds;
  243.         (**thePixMap).pmVersion = 0;
  244.         (**thePixMap).packType = 0;
  245.         (**thePixMap).packSize = 0;
  246.         (**thePixMap).hRes = kDefaultRes;
  247.         (**thePixMap).vRes = kDefaultRes;
  248.         (**thePixMap).pixelSize = depth;
  249.         (**thePixMap).planeBytes = 0;
  250.         (**thePixMap).pmReserved = 0;
  251.         if (depth <=8)
  252.         {
  253.             (**thePixMap).pixelType = 0;
  254.             (**thePixMap).cmpCount = 1;
  255.             (**thePixMap).cmpSize = depth;
  256.             (**thePixMap).pmTable = newColors;
  257.         } else {
  258.             (**thePixMap).pixelType = RGBDirect;
  259.             (**thePixMap).cmpCount = 3;
  260.             if (depth == 16)
  261.                 (**thePixMap).cmpSize = 5;
  262.             else
  263.                 (**thePixMap).cmpSize = 8;
  264.             (**newColors).ctSeed = 3 * (**thePixMap).cmpSize;
  265.             (**newColors).ctFlags = 0;
  266.             (**newColors).ctSize = 0;
  267.             (**thePixMap).pmTable = newColors;
  268.         }
  269.     } else
  270.         error = MemError();
  271. } else
  272.     newColors = nil;
  273. if(error != noErr)
  274. {
  275.     if (newColors != nil)
  276.     DisposeCTable(newColors);
  277. }
  278. return error;
  279. }
  280.